package com.hdey.service.max.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdey.mapper.max.TbUscSupplyMapper;
import com.hdey.pojo.max.TbUscSupcla;
import com.hdey.pojo.max.TbUscSupply;
import com.hdey.service.max.TbUscSupplySupclaSvs;
import com.hdey.service.max.TbUscSupplySvs;
import com.hdey.vo.SupplyVoForCreate;
import com.hdey.vo.SupplyVoForSearch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service("tbUscSupplySvs")
@Transactional
@Slf4j
public class TbUscSupplySvsImpl extends ServiceImpl<TbUscSupplyMapper, TbUscSupply> implements TbUscSupplySvs {


    @Resource
    private TbUscSupplyMapper supplyMapper;

    @Resource
    private TbUscSupplySupclaSvs supplySupclaSvs;


    //模糊查询
    @Override
    public List<TbUscSupply> searchSupply(SupplyVoForSearch supplyVo) {
        LambdaQueryWrapper<TbUscSupply> wrapper = new LambdaQueryWrapper<>();

        //id条件判断
        if(supplyVo.getSupplyId() != null && !"".equals(supplyVo.getSupplyId())){
            wrapper.eq(TbUscSupply::getSupplyId,supplyVo.getSupplyId());
        }

        //供应商名称条件判断
        if(supplyVo.getSupplyName()!= null && !"".equals(supplyVo.getSupplyName())){
            wrapper.eq(TbUscSupply::getSupplyName,supplyVo.getSupplyName());
        }


        //供应商资质期限条件判断  起始日期
        if(supplyVo.getStartSupplyDeadline()!= null && !"".equals(supplyVo.getStartSupplyDeadline())){
            System.out.println(supplyVo.getStartSupplyDeadline());
            wrapper.ge(TbUscSupply::getSupplyDeadline,supplyVo.getStartSupplyDeadline());
        }

        //供应商资质期限条件判断  截止日期
        if(supplyVo.getEndSupplyDeadline()!= null && !"".equals(supplyVo.getEndSupplyDeadline())){
            System.out.println(supplyVo.getEndSupplyDeadline());
            wrapper.le(TbUscSupply::getSupplyDeadline,supplyVo.getEndSupplyDeadline());
        }

        //供应商审核状态条件判断
        if(supplyVo.getSupplyUploadFlag()!= null && !"".equals(supplyVo.getSupplyUploadFlag()) && !"0".equals(supplyVo.getSupplyUploadFlag())){
            System.out.println(supplyVo.getSupplyUploadFlag());
            wrapper.eq(TbUscSupply::getSupplyUploadFlag,supplyVo.getSupplyUploadFlag());
        }

        //供应商分类条件判断
        if(supplyVo.getSupclaId()!= null && supplyVo.getSupclaId()!= 0 ){

            int supclaId = supplyVo.getSupclaId();
            wrapper.inSql(TbUscSupply::getSupplyId,
                    "SELECT SUPPLY_ID FROM TB_USC_SUPPLY_SUPCLA WHERE SUPCLA_ID = "+supclaId+" GROUP BY SUPPLY_ID");
        }

        List<TbUscSupply> list = supplyMapper.selectList(wrapper);

        return list;
    }

    @Override
    public int createSupply(SupplyVoForCreate supplyVo, List<TbUscSupcla> supclaList) {
        String supclaStr = "";

        TbUscSupply supply = new TbUscSupply();
        supply.setSupplyCode("null");
        supply.setSupplyName(supplyVo.getSupplyName());
        supply.setSupplyPass(supplyVo.getSupplyPwd());
        supply.setSupplyPhone("null");
        for(Integer i : supplyVo.getSupclaIds()){
            for(TbUscSupcla supcla : supclaList){
                if( i == supcla.getSupclaId()){
                    supclaStr += supcla.getSupclaName() + " ";
                }
            }
        }
        supclaStr.trim();
        supply.setSupclaCode(supclaStr);
        supply.setSupplyUploadFlag("A");
        supply.setSupplyFlag("Y");
        supply.setCreateBy("admin");

        int resoult = supplyMapper.insert(supply);

        //供应商分类对应表操作

        supplySupclaSvs.create(supply.getSupplyId(),supplyVo.getSupclaIds());

        return resoult;
    }


}
